package com.tb.rabbit.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

import static com.tb.rabbit.constant.RabbitConstant.*;

/**
 * 消息确认回调函数
 * @author tangbin
 */
@Slf4j
@Configuration
public class RabbitConfig {

    /**
     * 声明业务Exchange
     */
    @Bean("businessExchange")
    public FanoutExchange businessExchange(){
        //广播模式
        return  new FanoutExchange((BUSINESS_EXCHANGE_NAME));
    }
    /**
     * 声明死信Exchange
     */
    @Bean("deadLetterExchange")
    public DirectExchange deadLetterExchange(){
        //点对点模式模式
        return  new DirectExchange((DEAD_LETTER_EXCHANGE));
    }

    /**
     * 声明业务队列A
     * @return
     */
    @Bean("businessQueueA")
    public Queue businessQueueA(){
        HashMap<String, Object> map = new HashMap<>(2);
        map.put("x-dead-letter-exchange",DEAD_LETTER_EXCHANGE);
        map.put("x-dead-letter-routing-key",DEAD_LETTER_QUEUEA_ROUTING_KEY);
        return QueueBuilder.durable(BUSINESS_QUEUEA_NAME).withArguments(map).build();
    }

    /**
     * 声明业务延时队列B（延时60s），并绑定到对应的死信交换机
     * @return
     */
    @Bean("businessQueueB")
    public Queue businessQueueB(){
        HashMap<String, Object> map = new HashMap<>(2);
        // x-dead-letter-exchange 这里声明当前队列绑定的死信交换机
        map.put("x-dead-letter-exchange",DEAD_LETTER_EXCHANGE);

        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
        map.put("x-dead-letter-routing-key",DEAD_LETTER_QUEUEB_ROUTING_KEY);

        // x-message-ttl  声明队列的TTL
        map.put("x-message-ttl", 6000);
        return QueueBuilder.durable(BUSINESS_QUEUEB_NAME).withArguments(map).build();
    }
    /**
     * 声明死信队列A
     */
    @Bean("deadLetterQueueA")
    public Queue deadLetterQueueA(){
        return  new Queue(DEAD_LETTER_QUEUEA_NAME);
    }
    /**
     * 声明死信队列B
     */ @Bean("deadLetterQueueB")
    public Queue deadLetterQueueB(){
        return  new Queue(DEAD_LETTER_QUEUEB_NAME);
    }
    /**
     * 声明业务队列A绑定关系
     */
    @Bean
    public Binding businessBindingA(@Qualifier("businessQueueA") Queue queue,
                                    @Qualifier("businessExchange")FanoutExchange exchange){
        return BindingBuilder.bind(queue).to(exchange);
    }
    /**
     * 声明业务队列B绑定关系
     */
    @Bean
    public Binding businessBindingB(@Qualifier("businessQueueB") Queue queue,
                                    @Qualifier("businessExchange")FanoutExchange exchange){
        return BindingBuilder.bind(queue).to(exchange);
    }
    /**
     * 声明死信队列A绑定关系
     */
    @Bean
    public Binding deadLetterBindingA(@Qualifier("deadLetterQueueA")Queue queue,
                                      @Qualifier("deadLetterExchange")DirectExchange exchange)   {
        return BindingBuilder.bind(queue).to(exchange).with(DEAD_LETTER_QUEUEA_ROUTING_KEY);
    }
    /**
     * 声明死信队列B绑定关系
     */
    @Bean
    public Binding deadLetterBindingB(@Qualifier("deadLetterQueueB")Queue queue,
                                      @Qualifier("deadLetterExchange")DirectExchange exchange)   {
        return BindingBuilder.bind(queue).to(exchange).with(DEAD_LETTER_QUEUEB_ROUTING_KEY);
    }

//    @Bean
//    public RabbitTemplate createRabbitTemplate(ConnectionFactory connectionFactory) {
//        RabbitTemplate rabbitTemplate = new RabbitTemplate();
//        rabbitTemplate.setConnectionFactory(connectionFactory);
//        //设置开启Mandatory,才能触发回调函数,无论消息推送结果怎么样都强制调用回调函数
//        rabbitTemplate.setMandatory(true);
//
//        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
//            @Override
//            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
//                log.info("ConfirmCallback:     " + "相关数据：" + correlationData);
//                log.info("ConfirmCallback:     " + "确认情况：" + ack);
//                log.info("ConfirmCallback:     " + "原因：" + cause);
//            }
//        });
//
//        rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
//            @Override
//            public void returnedMessage(ReturnedMessage returnedMessage) {
//                log.info("ReturnCallback:     " + "消息：" + returnedMessage.getMessage());
//                log.info("ReturnCallback:     " + "回应码：" + returnedMessage.getReplyCode());
//                log.info("ReturnCallback:     " + "回应信息：" + returnedMessage.getReplyText());
//                log.info("ReturnCallback:     " + "交换机：" + returnedMessage.getExchange());
//                log.info("ReturnCallback:     " + "路由键：" + returnedMessage.getRoutingKey());
//            }
//        });
//        return rabbitTemplate;
//    }
}
